//
//  FoodScrollView.m
//  ArtOfLiving
//
//  Created by xalo on 16/4/30.
//  Copyright © 2016年 刘虎. All rights reserved.
//

#import "FoodScrollView.h"

@interface FoodScrollView ()

@property (nonatomic, strong) UIImageView *imageView;
@property (nonatomic, strong) UIPageControl *pageControl;
@property (nonatomic, strong) NSArray *pictureArr;//存储图片
@property (nonatomic, strong) NSTimer *timer;
@property (nonatomic, assign) NSInteger index;

@end

@implementation FoodScrollView

- (instancetype)initWithFrame:(CGRect)frame pictureArr:(NSArray *)pictureArray {
    
    self = [super initWithFrame:frame];
    if (self) {
       
        self.pictureArr = pictureArray;
        self.index = 0;
        [self addSubview:self.imageView];
        [self addSubview:self.pageControl];
        self.timer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(autoChangeImageAction:) userInfo:nil repeats:YES];
    }
    return self;
}

- (void)changePage:(UIPageControl *)sender {
    
    sender.currentPage = self.index;
}

//定时器回调
- (void)autoChangeImageAction:(NSTimer *)sender {
    
    CATransition *transition = [CATransition animation];
    transition.duration = 2.0;
    transition.type = @"cameralIrisHollowOpen";
    transition.subtype = kCATransitionFromBottom;
    [self.imageView.layer addAnimation:transition forKey:@"transition"];
    [self nextImage];
}

//图片的轮播切换
- (void)nextImage {
    
    self.index++;
    if (self.index == self.pictureArr.count-1) {
        self.index = 0;
    }
    SDWebImageOptions options = SDWebImageRetryFailed | SDWebImageLowPriority;
    [self.imageView sd_setImageWithURL:[NSURL URLWithString:self.pictureArr[self.index]] placeholderImage:[UIImage imageNamed:@"foodPlaceholdel.png"] options:options];
}

//轻拍手势回调
- (void)tapImageViewAction:(UITapGestureRecognizer *)sender {
    
    
}


#pragma mark - ------懒加载------
- (UIImageView *)imageView {
    
    if (!_imageView) {
        _imageView = [[UIImageView alloc]initWithFrame:self.bounds];
        SDWebImageOptions options = SDWebImageRetryFailed | SDWebImageLowPriority;
        [_imageView sd_setImageWithURL:[NSURL URLWithString:self.pictureArr.lastObject] placeholderImage:[UIImage imageNamed:@"foodPlaceholdel.png"] options:options];
        [_imageView setUserInteractionEnabled:YES];
        //图片添加轻拍手势
        UITapGestureRecognizer *tapImageView = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapImageViewAction:)];
        [_imageView addGestureRecognizer:tapImageView];
    }
    return _imageView;
}

- (UIPageControl *)pageControl {
    
    if (!_pageControl) {
        
        _pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(0, self.frame.size.height-30, kScreenWidth, 30)];
        _pageControl.currentPage = 0;
        _pageControl.numberOfPages = self.pictureArr.count;
        [_pageControl addTarget:self action:@selector(changePage:) forControlEvents:(UIControlEventValueChanged)];
    }
    return _pageControl;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
